package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.bean.LabelValueBean;
import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpMenu;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrganization;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpPrivilegeCheck;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpMenuService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpPrivilegeCheckService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpMenuVO;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpPrivilegeCheckVO;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.EasyExcelUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.google.common.collect.Lists;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author System
 */
@RestController
@RequestMapping("/admin/upmpprivilegecheck")
@Slf4j
public class UpmpPrivilegeCheckController  extends BaseController {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpPrivilegeCheckService upmpPrivilegeCheckService;

    @Autowired
    private IUpmpMenuService upmpMenuService;

    /**
     * 查询数据列表
     * @param upmpPrivilegeCheck
     * @return
     */
    @GetMapping
//    @RequiresPermissions("privilegecheck:list")
    public Map<String, Object> findPageList(UpmpPrivilegeCheckVO upmpPrivilegeCheck, QueryRequest queryRequest) {
        log.error("queryRequest.getPageNum:{}",queryRequest.getPageNum());
        return getDataTable(this.upmpPrivilegeCheckService.findPageList(upmpPrivilegeCheck,queryRequest));
    }
    /**
     * 查询制定权限的审核角色数据列表
     * @param upmpPrivilegeCheck
     * @return
     */
    @GetMapping("/getPrivilegeCheckRoles")
//    @RequiresPermissions(value = {"privilegecheck:add","privilegecheck:update"} ,logical = Logical.OR)
    public Map<String, Object> getPrivilegeCheckRoles(UpmpPrivilegeCheckVO upmpPrivilegeCheck) throws RedpException {
        return this.upmpPrivilegeCheckService.getPrivilegeCheckRoles(upmpPrivilegeCheck);
    }

    /**
     * 根据渠道类型查询菜单下来列表数据
     * @param upmpMenu
     * @return
     */
    @GetMapping("/findMenuList")
//    @RequiresPermissions(value = {"privilegecheck:add","privilegecheck:update"} ,logical = Logical.OR)
    public List<UpmpMenu> findMenuList(UpmpMenuVO upmpMenu) {
        return this.upmpMenuService.findMenuList(upmpMenu);
    }

    @Log("新增权限按钮审核配置")
    @PostMapping
//    @RequiresPermissions("privilegecheck:add")
    public void add(@Valid UpmpPrivilegeCheckVO upmpPrivilegeCheck) throws RedpException {
        try {
            this.upmpPrivilegeCheckService.add(upmpPrivilegeCheck);
        } catch (Exception e) {
            String message = "新增权限按钮审核配置";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除权限按钮审核配置")
    @DeleteMapping("/{privilegecheckIds}")
//    @RequiresPermissions("privilegecheck:delete")
    public void delete(@NotBlank(message = "{required}") @PathVariable String privilegecheckIds) throws RedpException {
        try {
            String[] ids = privilegecheckIds.split(StringPool.COMMA);
            this.upmpPrivilegeCheckService.delete(ids);
        } catch (Exception e) {
            String message = "删除权限按钮审核配置";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改权限按钮审核配置")
    @PutMapping
//    @RequiresPermissions("privilegecheck:update")
    public void update(@Valid UpmpPrivilegeCheckVO upmpPrivilegeCheck) throws RedpException {
        try {
            this.upmpPrivilegeCheckService.update(upmpPrivilegeCheck);
        } catch (Exception e) {
            String message = "修改权限按钮审核配置失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping("excel")
//    @RequiresPermissions("privilegecheck:export")
    public void export(UpmpPrivilegeCheck upmpPrivilegeCheck, HttpServletResponse response) throws RedpException {
        try {
            List<UpmpPrivilegeCheck> menus = this.upmpPrivilegeCheckService.findList(upmpPrivilegeCheck);
            EasyExcelUtil.export(response,menus, "权限审核配置管理" );
//            ExcelKit.$Export(UpmpPrivilegeCheck.class, response).downXlsx(menus, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    /**
     * 查询系统默认的审核层级列表
     *
     */
    @GetMapping("/getMaxCheckLevel/{instId}")
//    @RequiresPermissions(value={"privilegecheck:list","privilegecheck:add","privilegecheck:update"},logical = Logical.OR)
    public RedpResponse getMaxCheckLevel(@NotBlank(message = "{required}") @PathVariable String instId) {
        UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
        List<UpmpSysParam> instList = upmpSysParamService.findChildrenByPrimaryKey(UpmpConstant.SYS_INST_PARAM, defaultInst.getInstId(), null);        List<LabelValueBean> result = Lists.newArrayList();
        String maxRecords =UpmpConstant.DEFAULT_MAX_CHECK_LEVEL;
        if(!ObjectUtils.isEmpty(instList)){
            for(UpmpSysParam param:instList){
                if(instId.equals(param.getPrimaryKey())){
                    Map sysparam = (Map) JSON.parse(param.getRichtextValue());
                    maxRecords = ObjectUtils.isEmpty(sysparam.get(UpmpConstant.MAX_CHECK_LEVEL)) ? UpmpConstant.DEFAULT_MAX_CHECK_LEVEL : sysparam.get(UpmpConstant.MAX_CHECK_LEVEL).toString();
                    break;
                }
            }
        }
        return new RedpResponse().data(maxRecords);
    }
}
